setClass("IIClusterScore",contains="InternalClusterScore",
		prototype=prototype(
				.description="I Cluster Internal Score class"
		
		)
)  

#global functions

#helper functions


#methods

#calclustes silhouette index for given clusterization
setMethod("ScoreSet",
		signature="IIClusterScore",
		definition=function(.Object,inputSet,clusters,...){
			#check for X and clusters
			callNextMethod(.Object,inputSet,clusters,...)
			if(isOneCluster(clusters))return(-.Machine$double.xmax)
			
			p <-2 # this value is used very often
			
			#calclulate number of clusters
			c <- length(unique(clusters))
			
			#calculate centers of clusters
			#each row contains cluster center
			#print("calculate centers");print(c)
			cluCentsMatrix <- matrix(unlist(lapply(1:c,FUN=function(n,clusters,points){
									
									clusterElements <- which(clusters==n)
									clusterElementsNumber <- length(clusterElements)
									
									clusterCenter <- colSums(points[clusterElements,,drop=FALSE])/clusterElementsNumber
										
									},clusters,inputSet$X)
							),nrow=c,byrow=TRUE)
			#Bc <- max distance between cluster centers
			
			Bc <- max(dist(cluCentsMatrix))
			#print(dim(cluCentsMatrix)) 
			
			#Wc <- sum of  cluster points distances to cluster centers
			
			Wc <- sum( unlist(lapply(1:c,
									FUN=function(n,points,clCenters){
										#print("clust EL")
										clusterElements <- which(clusters==n)
										#print("Clust cent")
										cCenter <- clCenters[n,]
										#print("befire sweep")
										sum( sqrt( rowSums( sweep(points[clusterElements,,drop=FALSE],2,cCenter,"-" )^2 ) ) )
										
									},
									inputSet$X,cluCentsMatrix) ) )
			Wc <- ifelse(Wc==0,1,Wc)
			#calculate overall mean point
			
			meanPoint <- colSums(inputSet$X)/dim(inputSet$X)[1]
			
			#Wi calculate distances sum from all points to overall mean point
			
			Wi <- sum( sqrt( colSums( sweep(inputSet$X,2,meanPoint,"-")^2 ) ) )
			
			Iindex <- {{Wi*Bc}/{c*Wc}}^p
			
			return(Iindex) #max
		}
)  

